//
//  ByTabBarVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/3.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByTabBarVC.h"
#import "ByTabBar.h"

#import "ByHomeVC.h"
#import "ByFoundVC.h"
#import "ByDocumentaryVC.h"
#import "ByLotteryNoticeVC.h"
#import "ByPersonalCenterVC.h"


@interface ByTabBarVC ()

@property (nonatomic, strong) ByTabBar *bar;

@end

@implementation ByTabBarVC

+ (ByTabBarVC *)shareSigleton{
    UIApplication *appliction = [UIApplication sharedApplication];
    AppDelegate *delegate =(AppDelegate*) appliction.delegate;
    ByTabBarVC * tabBarVC = (ByTabBarVC*)delegate.window.rootViewController ;
    return tabBarVC;
}


- (void)viewDidLoad {
    
    self.tabBar.hidden = YES;
    [self initCustomTabBar];
    [self createViewControllers];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

// 创建自定义bar
- (void)initCustomTabBar {
    
    [self.view addSubview:self.bar];
    [self.bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(49);
        if (@available(ios 11.0,*)) {
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.bottom.equalTo(self.view);
        }
    }];
    
    self.bar.images = @[@"shouyeweixuanzhong",@"faxian",@"gendan 拷贝",@"gonggaoweixuanzhong",@"wode 拷贝"];
    self.bar.selectedImages = @[@"shouyexuanzhong",@"faxianxuanzhong",@"gendan-1",@"gonggaoxuanzhong",@"wode"];
    self.bar.titles = @[@"首页",@"发现",@"跟单",@"开奖公告",@"我的彩票"];
    [self.bar setupUI];
}
// 创建自控制器
- (void)createViewControllers{
    ByHomeVC *vc1 =  [[ByHomeVC alloc] init];
    ByBaseNaviController *nav1 = [[ByBaseNaviController alloc] initWithRootViewController:vc1];
    ByFoundVC *vc2 = [[ByFoundVC alloc] init];
    ByBaseNaviController *nav2 = [[ByBaseNaviController alloc] initWithRootViewController:vc2];
    ByDocumentaryVC *vc3 = [[ByDocumentaryVC alloc] init];
    ByBaseNaviController *nav3 = [[ByBaseNaviController alloc] initWithRootViewController:vc3];
    ByLotteryNoticeVC *vc4 = [[ByLotteryNoticeVC alloc] init];
    ByBaseNaviController *nav4 = [[ByBaseNaviController alloc] initWithRootViewController:vc4];
    ByPersonalCenterVC *vc5 = [[ByPersonalCenterVC alloc] init];
    ByBaseNaviController *nav5 = [[ByBaseNaviController alloc] initWithRootViewController:vc5];
    self.viewControllers = @[nav1,nav2,nav3,nav4,nav5];
    
}


- (ByTabBar *)bar {
    
    if (!_bar) {
        _bar = [[ByTabBar alloc] init];
        __weak typeof(self) wself = self;
        _bar.selectBlock = ^(NSInteger index) {
            [wself selectItemWithIndex:index];
        };
    }
    return _bar;
}


#pragma mark - 点击事件
- (void)selectItemWithIndex:(NSInteger)index {
    self.selectedIndex = index;
}

#pragma mark - 标签栏隐藏和显示
// 隐藏标签栏
- (void)hideTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.bar mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(49);
            if (@available(ios 11.0,*)) {
                make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
                make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
            } else {
                make.left.equalTo(self.view);
                make.right.equalTo(self.view);
                make.top.equalTo(self.view.mas_bottom).offset(0);
            }
        }];
        
    }];
}


// 标签栏出现
- (void)showTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.bar mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(49);
            if (@available(ios 11.0,*)) {
                make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
                make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            } else {
                make.left.equalTo(self.view);
                make.right.equalTo(self.view);
                make.bottom.equalTo(self.view);
            }
            
        }];
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
